//
//  WebViewVC.m
//  断点下载PDF
//
//  Created by 徐凯 on 16/5/13.
//  Copyright © 2016年 cqjtu. All rights reserved.
//

#import "WebViewVC.h"
#import "ConvertData.h"
#import "TestObject.h"
@implementation WebViewVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    UIButton* TestBtn = [UIButton  buttonWithType:UIButtonTypeRoundedRect];
    TestBtn.frame = CGRectMake(10, 100, 100, 44);
    [TestBtn setTitle:@"删除" forState:UIControlStateNormal];
    [TestBtn addTarget:self action:@selector(deleteDL) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:TestBtn];
    
    self.view.backgroundColor = [UIColor whiteColor];
    _downloadObjectArr = [NSMutableArray arrayWithArray:[WHC_DownloadObject readDiskAllCache]];
   // [self.delegate updateDownloadValue:<#(WHC_DownloadObject *)#> index:<#(NSInteger)#>]
    [self initDownLoadDelete];
   
}

-(void)initDownLoadDelete{
    
    _cellObjectArr = [[NSMutableArray alloc]init];
    NSLog(@"json=%@",_downloadObjectArr );
    TestObject* obj = [[TestObject alloc]init];
    obj.array = _downloadObjectArr;
    
    for (int i = 0; i < _downloadObjectArr.count; i++) {
        DownLoadOpration* opration = [[DownLoadOpration alloc] init];
        [opration initWithDownloadObject:_downloadObjectArr[i] index:i];
        NSData* jsonData = [ConvertData getJSON:obj options:NSJSONWritingPrettyPrinted error:nil];
        NSString* jsonText = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        NSLog(@"json=%@",jsonText);
        opration.delegate = self;
        [_cellObjectArr addObject:opration];
    }
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
}

#pragma mark 更新页面元素状态
-(void)updatePageElementState:(WHC_DownloadObject*)object index:(NSInteger)index{
    //NSLog(@"当前在主界面的下载进度%f",downloadObject.downloadProcessValue);

    switch (object.downloadState) {
        case WHCDownloadWaitting:
            NSLog(@"正在等待！");
            break;
        case WHCDownloading:
            NSLog(@"正在下载!");
            break;
        case WHCDownloadCompleted:
            NSLog(@"下载完成");
            break;
        case  WHCDownloadCanceled:
            NSLog(@"已暂停下载");
            break;
        case WHCNone:
            NSLog(@"什么操作都没有");
            break;
        default:
            break;
    }
    
}
#pragma mark 点击暂停 开始 取消 等待 操作
-(void)clickBtn:(NSInteger)index{
        
    DownLoadOpration* opration = _cellObjectArr[index];
    [opration clickElmentReponse];
    
}

-(void)deleteDownLoad:(NSInteger)index{
    
     WHC_DownloadObject * downloadObject = _downloadObjectArr[index];
    [[WHC_HttpManager shared] cancelDownloadWithFileName:downloadObject.fileName deleteFile:YES];
    [downloadObject removeFromDisk];
    [_downloadObjectArr removeObjectAtIndex:index];
    [_cellObjectArr removeObjectAtIndex:index];
    
}

#pragma mark - DownLoadDelegate
- (void)pdfDownload:(NSError *)error index:(NSInteger)index strUrl:(NSString *)strUrl {
    
    if (error != nil) {
        [self.view toast:error.userInfo[NSLocalizedDescriptionKey]];
    }
    WHC_DownloadObject * downloadObject = _downloadObjectArr[index];
    [downloadObject removeFromDisk];
    [_downloadObjectArr removeObjectAtIndex:index];
    //[_offLineTableView reloadData];
}

- (void)updateDownloadValue:(WHC_DownloadObject *)downloadObject index:(NSInteger)index {
    
    if (downloadObject != nil) {
        WHC_DownloadObject * tempDownloadObject = _downloadObjectArr[index];
        tempDownloadObject.currentDownloadLenght = downloadObject.currentDownloadLenght;
        tempDownloadObject.totalLenght = downloadObject.totalLenght;
        tempDownloadObject.downloadSpeed = downloadObject.downloadSpeed;
        tempDownloadObject.downloadState = downloadObject.downloadState;
        [self updatePageElementState:tempDownloadObject index:index];
    }
}
///下载进度回调函数
- (void)downloadProgress:(WHC_DownloadObject *)downloadObject index:(NSInteger)index{
    
    if (downloadObject != nil) {
        NSLog(@"当前在主界面的下载进度%f",downloadObject.downloadProcessValue);
        [self updatePageElementState:downloadObject index:index];
    }
}
- (void)downloadFinish:(WHC_DownloadObject *)downloadbject index:(NSInteger)index{
    if (downloadbject != nil) {
        [self updatePageElementState:downloadbject index:index];
    }
}


-(void)deleteDL{
    [self deleteDownLoad:0];
}

@end
